<!--车辆管理页-->
<template>
  <div style="height:6vh;padding:10px 20px;background-color:#fff;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button type="primary" @click="saveDialogTitle"
               style="float:right;margin-top:10px;">新建车辆</el-button>
  </div>
  <!-- 新建车辆弹窗 -->
  <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      style="width: 1000px;padding: 40px;"
      :before-close="handleClose">

    <el-form label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.assetBrand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="车辆型号" >
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="座位数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.seat"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.assetCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车保编号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.insurance"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="所属地">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.assetLocation"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车用能源" >
            <el-select placeholder="请选择" v-model="saveVehicleForm.energy">
              <el-option label="油车" value="1"></el-option>
              <el-option label="电车" value="2"></el-option>
              <el-option label="油电混合" value="3"></el-option>
              <el-option label="其它" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <el-option label="轿车" value="10"></el-option>
              <el-option label="客车" value="20"></el-option>
              <el-option label="货车" value="30"></el-option>
              <el-option label="电单车" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资产状态">
            <el-select placeholder="请选择" v-model="saveVehicleForm.assetStatus">
              <el-option label="空闲" value="1"></el-option>
              <el-option label="占用" value="2"></el-option>
              <el-option label="待修" value="3"></el-option>
              <el-option label="修理中" value="4"></el-option>
              <el-option label="报废" value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="使用者员工编号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.userId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门编号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.departmentId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.assetPrice"
                      :formatter="(value) => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
                      :parser="(value) => value.replace(/\$\s?|(,*)/g, '')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.assetBuyTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary"
                 @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!--车辆搜索卡片-->
  <el-card style="margin: 20px;height: 70px">
    <el-form :inline="true">
      <el-form-item label="车辆品牌" style="width: 200px;">
        <el-input placeholder="请输入品牌" v-model="searchVehicleForm.assetBrand"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width: 200px;">
        <el-input placeholder="请输入车牌" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item label="车辆识别码" style="width: 200px;">
        <el-input placeholder="请输入识别码" v-model="searchVehicleForm.assetCode"></el-input>
      </el-form-item>
      <el-form-item label="车辆类型" style="width: 170px;">
        <el-select placeholder="请选择" v-model="searchVehicleForm.type">
          <el-option label="轿车" value=10></el-option>
          <el-option label="客车" value=20></el-option>
          <el-option label="货车" value=30></el-option>
          <el-option label="电单车" value=40></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产状态" style="width: 170px;">
        <el-select placeholder="请选择" v-model="searchVehicleForm.assetStatus">
          <el-option label="空闲" value=1></el-option>
          <el-option label="占用" value=2></el-option>
          <el-option label="待修" value=3></el-option>
          <el-option label="修理中" value=4></el-option>
          <el-option label="报废" value=5></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleTableDate">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="assetBrand" label="车辆品牌"  align="center"/>
      <el-table-column prop="license" label="车牌号"  align="center"/>
      <el-table-column prop="model" label="车牌型号"  align="center"/>
      <el-table-column prop="seat" label="座位数"  align="center"/>
      <el-table-column prop="assetCode" label="车辆识别码"  align="center"/>
      <el-table-column prop="insurance" label="车保编号"  align="center"/>
      <el-table-column prop="type" label="车辆类型"  align="center"
                       :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="energy" label="车用能源"  align="center"
                       :formatter="energyTypeFormatter"/>
      <el-table-column prop="assetStatus" label="资产状态"  align="center"
                       :formatter="assetFormatter"/>
      <el-table-column prop="userId" label="使用者编号" align="center"/>
      <el-table-column prop="departmentId" label="部门编号" align="center"/>
      <el-table-column prop="remark" label="备注" align="center"/>
      <el-table-column prop="assetBuyTime" label="购买时间" align="center"/>
      <el-table-column label="操作" align="center" >
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id,scope.row.assetCode)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:30px;float: right;">
    <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5, 10, 15, 20]"
        v-model:current-page="currentPage"
        v-model:page-size="currentSize"
        v-model:total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    </div>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
// import axios from "axios";
import axios from "@/http";
import qs from "qs";
import {ElMessage} from "element-plus";

const currentPage = ref(1);
const currentSize = ref(5);
const total = ref(100);
const userInfo = getUser();
const username = userInfo?userInfo.username:'用户';
var id = userInfo.id;
var departmentId = userInfo.departmentId;


//修改每页显示多少条，触发此函数
const handleSizeChange = (val) => {
  loadVehicle();
}
//修改当前页码，触发此函数
const handleCurrentChange = (val) => {
  loadVehicle();
}

//搜索
const searchVehicleForm = ref({
  assetBrand:'',
  license:'',
  assetCode:'',
  type:'',
  status:''
});
//保存查询列表
const vehicleTableDate = ref([]);

const loadVehicle = () => {
  searchVehicleForm.value.pageSize = currentSize.value;
  searchVehicleForm.value.pageNum = currentPage.value;
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+'/oa/asset/vehicle/select?'+data).then((response)=>{
    if(response.data.code === 20000){
      vehicleTableDate.value = response.data.data.list;
      total.value = response.data.data.total;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

const resetSearch = () =>{
  searchVehicleForm.value = {};
  loadVehicle();
}

onMounted(()=>{
  loadVehicle();
})

const dialogTitle = ref('新建车辆');
const dialogVisible = ref(false);
const saveDialogTitle = () => {
  dialogVisible.value = true;
}

const saveVehicleForm = ref({});

const vehicleTypeFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = '轿车';
  }else if(cellValue == 20){
    cellValue = '客车';
  }else if(cellValue == 30){
    cellValue = '货车';
  }else if(cellValue == 40){
    cellValue = '电单车';
  }
  return cellValue;
}
const energyTypeFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '油车';
  }else if(cellValue == 2){
    cellValue = '电车';
  }else if(cellValue == 3){
    cellValue = '油电混合';
  }else if(cellValue == 4){
    cellValue = '其它';
  }
  return cellValue;
}
const assetFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '空闲';
  }else if(cellValue == 2){
    cellValue = '占用';
  }else if(cellValue == 3){
    cellValue = '待修';
  }else if(cellValue == 4){
    cellValue = '修理中';
  }else if(cellValue == 5){
    cellValue = '报废';
  }
  return cellValue;
}

const saveVehicle = () => {
  let data = qs.stringify(saveVehicleForm.value);
  axios.post(BASE_URL+'/oa/asset/vehicle/save',data).then((response)=>{
    if(response.data.code == 20000){
      ElMessage.success("保存成功");
      loadVehicle();
      dialogVisible.value = false;
      saveVehicleForm.value = {};
      vehicle();
    }else {
      ElMessage.error(response.data.msg)
    }
  })
}

const handleClose = () => {
  if(confirm('确定要关闭此弹窗?')){
    dialogVisible.value = false;
    saveVehicleForm.value = {};
    loadVehicle();
  }
}

const editVehicle = (id) => {
  dialogTitle.value = '管理车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/oa/asset/vehicle/select?id='+id).then((response)=>{
    if(response.data.code === 20000){
      saveVehicleForm.value = response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

const deleteVehicle = (id,assetCode) => {
  if(confirm('确定要删除此车辆吗?')){
    axios.post(BASE_URL+'/oa/asset/vehicle/delete/'+id+'/'+assetCode).then((response)=>{
      if(response.data.code == 20000){
        ElMessage.success("删除成功");
        loadVehicle();
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

const vehicle = () => {
  axios.get(BASE_URL+'/oa/asset/vehicle/addNum').then((response)=>{
  })
}

</script>

<style scoped>

</style>